<template>
  <div class="registerbox">
    <el-card>
      <el-button class="fr" @click="toBack()">取消</el-button>
      <el-button class="fr" type="primary" @click="handleSubmit"
        >提交</el-button
      >
      <h3>新建耗材入库</h3>
    </el-card>
    <div class="ml-30 mr-30">
      <el-row>
        <el-card class=" mr20 mt-30">
          <el-form ref="form" :model="form" class="form">
            <el-row>
              <el-col :span="12">
                <el-form-item label="入库单编号：">
                  <el-input v-model="form.storeNo"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="应用科室" prop="deptId" label-width="20%">
                  <treeselect
                    v-model="form.deptId"
                    :options="deptOptions"
                    :show-count="true"
                    placeholder="请选择归属部门"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="入库时间：">
                  <el-date-picker
                    v-model="form.createTime"
                    type="date"
                    value-format="yyyy-MM-dd HH:mm:ss"
                  >
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="备注：">
                  <el-input v-model="form.remark"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <p>入库详细信息</p>
          <el-divider />
          <el-button class="fr" type="primary" @click="handleAdd"
            >新增一行</el-button
          >
          <el-table :data="tableData" style="width: 100%" border>
            <el-table-column prop="materialNo" label="耗材编号">
              <template slot-scope="scope">
                <el-select
                  v-model="scope.row.id"
                  placeholder="请选择检验项"
                  class="td-input"
                >
                  <el-option
                    v-for="item in materialList"
                    :key="item.id"
                    :label="item.materialNo"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </template>
            </el-table-column>

            <el-table-column prop="materialId" label="耗材编号">
              <template slot-scope="scope">
                <!--                <el-input v-model="scope.row.name" class="td-input"/>-->
                {{ handleFilterEname(scope.row.id) }}
              </template>
            </el-table-column>
            <el-table-column prop="materialName" label="耗材名称">
              <template slot-scope="scope">
                <!--                <el-input v-model="scope.row.name" class="td-input"/>-->
                {{ handleFilterEname(scope.row.id) }}
              </template>
            </el-table-column>

            <el-table-column prop="batchNo" label="耗材批号">
              <template slot-scope="scope">
                <el-input v-model="scope.row.batchNo" class="td-input" />
                <!--                {{ handleFilterEname(scope.row.materialNo) }}-->
              </template>
            </el-table-column>

            <el-table-column prop="type" label="耗材类型">
              <template slot-scope="scope">
                <!--                <el-input v-model="scope.row.type" class="td-input"/>-->
                {{ handleFilterType(scope.row.id) }}
              </template>
            </el-table-column>

            <el-table-column prop="manufacturer" label="生产厂商">
              <template slot-scope="scope">
                <!--                <el-input v-model="scope.row.tradeName" class="td-input"/>-->
                {{ handleFilterManufacturer(scope.row.id) }}
              </template>
            </el-table-column>

            <el-table-column prop="manufacturingDate" label="生产日期">
              <template slot-scope="scope">
                <!--                <el-input v-model="scope.row.manufacturingDate" class="td-input"/>-->
                <el-date-picker
                  v-model="scope.row.manufacturingDate"
                  type="date"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  class="td-input"
                >
                </el-date-picker>
              </template>
            </el-table-column>

            <el-table-column prop="shelflife" label="失效日期">
              <template slot-scope="scope">
                <!--                <el-input v-model="scope.row.expiryDate" class="td-input"/>-->
                {{ handleFilterShelflife(scope.row.id) }}
              </template>
            </el-table-column>

            <el-table-column prop="count" label="入库数量">
              <template slot-scope="scope">
                <el-input v-model="scope.row.count" class="td-input" />
              </template>
            </el-table-column>

            <el-table-column prop="unit" label="单位">
              <template slot-scope="scope">
                <el-input v-model="scope.row.unit" class="td-input" />
              </template>
            </el-table-column>

            <el-table-column label="操作" align="center">
              <template slot-scope="scope">
                <el-button
                  @click="addJurisdiction(scope.$index, scope.row)"
                  type="primary"
                  >复制行
                </el-button>

                <el-button
                  type="danger"
                  @click="handleDelete(scope.$index, scope.row)"
                  class="del-btn"
                  >删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-row>
    </div>
  </div>
</template>

<script>
import { treeselect } from "@/api/system/dept";
import { listMaterial } from "../../../../api/business/material";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import Treeselect from "@riophae/vue-treeselect";
import {
  addMaterialStore,
  updateMaterialStore
} from "../../../../api/business/materialStore";

export default {
  name: "addEdit",
  components: { Treeselect },
  props: {},
  data() {
    return {
      loading: false,
      deptOptions: undefined,
      materialList: [],
      checkList: [],
      form: {
        storeNo: "",
        deptId: 100,
        createTime: undefined,
        remark: ""
      },
      formAdd: {},
      tableData: [],
      total: 1,
      pageNum: 1,
      pageSize: 20,
      radio: 1
    };
  },
  created() {
    this.getTreeSelect();
    this.queryMaterialList();
  },
  methods: {
    getTreeSelect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
      });
    },
    handleFilterEname(id) {
      if (id) {
        return this.materialList.filter(x => x.id == id)[0].materialName;
      }
    },
    handleFilterShelflife(id) {
      if (id) {
        return this.materialList.filter(x => x.id == id)[0].shelflife;
      }
    },
    handleFilterManufacturer(id) {
      if (id) {
        console.log(this.materialList);
        return this.materialList.filter(x => x.id == id)[0].manufacturer;
      }
    },
    handleFilterType(id) {
      if (id) {
        return this.materialList.filter(x => x.id == id)[0].type;
      }
    },
    handleAdd() {
      let row = {
        id: "",
        materialId: "",
        materialNo: "",
        materialName: "",
        batchNo: "",
        type: "",
        manufacturer: "",
        manufacturerDate: "",
        shelflife: "",
        count: "",
        unit: ""
      };
      this.tableData.push(row);
    },
    handleDelete(index, row) {
      this.tableData.splice(index, 1);
    },
    addJurisdiction(index, row) {
      this.tableData.push(row);
    },
    onSubmit() {},
    toBack() {
      this.$emit("func", false);
    },
    queryMaterialList() {
      listMaterial().then(response => {
        this.materialList = response.rows;
      });
    },
    handleSubmit() {
      if (!this.form.storeNo) {
        this.$message.warning("入库单编号不能为空");
        return;
      }
      if (!this.form.deptId) {
        this.$message.warning("所属科室不能为空");
        return;
      }
      if (this.tableData) {
        this.tableData.map((item, index) => {
          if (!item.id) {
            this.$message.warning("耗材编号不能为空");
            return;
          }
          item.storeNo = this.form.storeNo;
          item.deptId = this.form.deptId;
          // item.createTime = this.form.createTime;
          item.remark = this.form.remark;
          item.materialId = item.id;
          // item.materialName =this.materialList.filter(x => x.id == item.id)[0].materialName;
          // item.shelflife =this.materialList.filter(x => x.id == item.id)[0].shelflife;
          // item.manufacturer =this.materialList.filter(x => x.id == item.id)[0].manufacturer;
          // item.type =this.materialList.filter(x => x.id == item.id)[0].type;
          console.log(item);
          if (item.id) {
            // if (index >= 1) {
            item.id = "";
            addMaterialStore(item).then(res => {
              this.handleClose();
            });
            // }
            // else {
            //   updateMaterialStore(item).then((res) => {
            //     this.handleClose()
            //   })
            // }
          } else {
            addMaterialStore(item).then(res => {
              this.handleClose();
            });
          }
        });
      } else {
        this.$message.warning("请输入检测项");
        return;
      }
    },
    handleClose() {
      this.id = null;
      this.$emit("func", false);
    }
  }
};
</script>

<style lang="scss" scoped>
.td-input {
  width: 80px !important;
}
</style>
